package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.service.impl.Gwai800ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 年返奖励报表
 * Created by shixc on 2019-5-10
 */
@Controller
@RequestMapping("/crm/gwai800")
public class Gwai800Controller extends BaseController {

    @Autowired
    private Gwai800ServiceImpl gwai800Service;

    @Autowired
    public SysAuthorityService sysAuthorityService;

    /**
     * 跳转年返奖励报表页面
     */
    @RequiresPermissions("crm:gwai800:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai800.html";
    }

    /**
     * 查询年返奖励报表列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwai800:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = new ArrayList<>();
        if (inDto.getString("COMPANY") == null || inDto.getString("COMPANY") == "") {
            return new PageReData(page, result);
        }
        if (inDto.getString("GWDD012").equals("")) {
            //客户权限
            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
        } else {
            inDto.put("CUSTOMERS", "");
        }
        result = gwai800Service.getGwddData(page, inDto);
        if (result.size() > 0) {
            for (int i = 0; i < result.size(); i++) {
                Dto gwddDto = result.get(i);
                if (inDto.getString("GWDD012").equals("")) {
                    inDto.put("GWDD012", gwddDto.getString("GWDD012"));
                }
                Dto gwalDto = this.gwai800Service.getGwal(inDto);
                if (gwalDto != null) {
                    result.get(i).put("GWADL004", gwalDto.getString("GWADL004"));//省份(联系人所属地区)
                }
                inDto.put("START", inDto.getString("START").replaceAll("-", ""));
                inDto.put("END", inDto.getString("END").replaceAll("-", ""));
                BigDecimal T1 = new BigDecimal(0);
                BigDecimal T2 = new BigDecimal(0);
                BigDecimal T4 = new BigDecimal(0);
                BigDecimal T5 = new BigDecimal(0);
                BigDecimal T3 = new BigDecimal(0);
                BigDecimal T6 = new BigDecimal(0);
                Dto maDto = this.gwai800Service.getMA041(inDto);
                if (maDto != null) {
                    result.get(i).put("MA041", maDto.getString("MA041"));//结算方式
                }
                if ("1".equals(gwddDto.getString("GWDE011")) || "3".equals(gwddDto.getString("GWDE011"))) {//返利编号净发货额
                    Dto dto = this.gwai800Service.getFH(inDto);
                    result.get(i).put("FH", dto.getString("FH"));//发货金额
                    result.get(i).put("TH", dto.getString("TH"));//退货金额
                    T1 = dto.getBigDecimal("FH").subtract(dto.getBigDecimal("TH"));
                } else {
                    result.get(i).put("FH", 0);
                    result.get(i).put("TH", 0);
                }
                if ("1".equals(gwddDto.getString("GWDE012")) || "3".equals(gwddDto.getString("GWDE012"))) {//返利编号无返利发货额
                    Dto dto = this.gwai800Service.getWFLFH(inDto);
                    result.get(i).put("WFLFH", dto.getString("WFLFH"));//无返利发货
                    T2 = dto.getBigDecimal("WFLFH");
                } else {
                    result.get(i).put("WFLFH", 0);
                }
                if ("1".equals(gwddDto.getString("GWDE014")) || "3".equals(gwddDto.getString("GWDE014"))) {//铺货
                    Dto dto = this.gwai800Service.getPH(inDto);
                    result.get(i).put("PHFH", dto.getString("PHFH"));//铺货发货
                    result.get(i).put("PHHK", dto.getString("PHHK"));//铺货回款
                    result.get(i).put("PHWFL", dto.getString("PHWFL"));//铺货无返利回款
                    T4 = dto.getBigDecimal("PHFH").subtract(dto.getBigDecimal("PHHK")).subtract(dto.getBigDecimal("PHWFL"));
                } else {
                    result.get(i).put("PHFH", 0);
                    result.get(i).put("PHHK", 0);
                    result.get(i).put("PHWFL", 0);
                }
                if ("2".equals(gwddDto.getString("GWDE013")) || "3".equals(gwddDto.getString("GWDE013"))) {//不计入指标产品
                    List<Dto> list = this.gwai800Service.getGwamData(inDto); //产品
                    inDto.put("GWAM", list);
                    if (list.size() > 0) {
                        Dto dto = this.gwai800Service.getCPHJ(inDto);
                        Dto list1 = this.gwai800Service.getCPMX(inDto);//产品明细
                        result.get(i).put("CPHJ", dto.getString("CPHJ"));//不计入指标产品合计
                        result.get(i).put("CPMX", list1.getString("result"));//不计入指标产品明细
                        T3 = dto.getBigDecimal("CPHJ");
                    } else {
                        result.get(i).put("CPHJ", 0);
                        result.get(i).put("CPMX", "");
                    }
                } else {
                    result.get(i).put("CPHJ", 0);
                    result.get(i).put("CPMX", "");
                }
                Dto tkDto = this.gwai800Service.getTZHJ(inDto);
                result.get(i).put("TZHJ", tkDto.getString("TZHJ"));//系统外调整合计
                result.get(i).put("TZMX", tkDto.getString("TZMX"));//系统外调整明细
                T6 = tkDto.getBigDecimal("TZHJ");
                BigDecimal ZBJE = new BigDecimal(0);
                ZBJE = T1.subtract(T4).subtract(T3).subtract(T6);
                result.get(i).put("ZBJE", ZBJE);//指标金额
                result.get(i).put("YJFH", T1.subtract(T2).subtract(T4).subtract(T3).subtract(T6));//应记发货金额
                inDto.put("GWDD001", result.get(i).getString("GWDD001"));
                inDto.put("GWDE003", result.get(i).getString("GWDE003"));
                List<Dto> gwdd019 = this.gwai800Service.getGWDD019(inDto);
                if (gwdd019.size() > 0) {
                    result.get(i).put("GWDD019", gwdd019.get(0).getBigDecimal("GWDD019"));
                }
                BigDecimal rate = result.get(i).getBigDecimal("GWDD019").divide(new BigDecimal(100));
                BigDecimal count = T6.multiply(rate);
                result.get(i).put("FL", T1.subtract(T2).subtract(T4).subtract(T3).subtract(count));//返利金额
            }
        }
        return new PageReData(page, result);
    }

    /**
     * 批量确认数据
     */
    @RequestMapping("/getGwdeData")
    @RequiresPermissions("crm:gwai800:init")
    @ResponseBody
    public ResponseData getGwdeData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> list = this.gwai800Service.getGwdeData(inDto);
        return new SuccessResponseData(list);
    }

}
